<?php

require( "db.inc" );

define( "LOGGER_TIMEZONE", "America/Chicago" );
define( "LOGGER_SQL_DATE_FORMAT", "Y-m-d H:i:s" );
define( "LOGGER_REVISION", "2" );

function LoggerOpenDB()
{
	$link = mysql_connect( LOGGER_DB_HOST, LOGGER_DB_USER, LOGGER_DB_PASSWORD );
	if ( $link )
	{
		mysql_select_db( LOGGER_DB_NAME );
	}
	
	return $link;
}

function LoggerCreateXMLDocument()
{
	$doc = new DOMDocument();
	$topNode = $doc->appendChild( $doc->createElement( "results" ) );
	$topNode->appendChild( $doc->createElement( "revision", LOGGER_REVISION ) );
	$doc->formatOutput = true;
	return $topNode;
}

function LoggerAppendError( &$topNode, $error )
{
	$doc = $topNode->ownerDocument;
	$topNode->appendChild( $doc->createElement( "error", $error ) );
}

function LoggerAppendSelect( $sql, $tag, &$topNode )
{
	$doc = $topNode->ownerDocument;
	$data = mysql_query( $sql );
	if ( $data )
	{
		while ( $row = mysql_fetch_assoc( $data ) )
		{
			$new_node = $topNode->appendChild( $doc->createElement( $tag ) );
			
			foreach ( $row as $column => $value )
			{
				if ( !is_null( $value ) )
					$new_node->appendChild( $doc->createElement( $column, htmlspecialchars( utf8_encode( stripslashes( $value ) ) ) ) );
			}
		}
	}
	else
	{
		LoggerAppendError( $topNode, "Query failure for tag $tag" );
	}
}

?>